﻿using Microsoft.AspNetCore.Mvc;
using Single.Core.Service;
using Single.WebApp.Components;

namespace Single.WebApp.Areas.Sys.Controllers
{
    [Area("Sys")]
    public class DevelopController : WorkController
    {
        public IActionResult GenerateEntity()
        {
            return View();
        }

        public string GetEntityName(string tableName)
        {
            return ServiceSysProps.SysOther.GetEntityInfo(tableName).Serialize();
        }

        public string DoGenerateEntity(string tableName, string area, string tenant, string entity)
        {
            var columnsData = ServiceSysProps.SysOther.GetColumnsDataByTableName(tableName);

            var columns = columnsData.Data as Dictionary<string, string>;

            ServiceResult result;

            using (var g = new Generate.Generate("WebApp"))
            {
                var geResult = g.GenerateEntity(area, tenant, entity, columns);

                result = new ServiceResult(geResult.Success, geResult.Message);
            }

            return result.Serialize();
        }

        public IActionResult GenerateService()
        {
            return View();
        }

        public string GetServicesInfo()
        {
            object rows;

            using (var g = new Generate.Generate("WebApp"))
            {
                rows = g.GetProjectsInfo();
            }

            return ServiceResult.SerializeData(new { rows });
        }

        public string DoGenerateService(string name)
        {
            ServiceResult result;

            using (var g = new Generate.Generate("WebApp"))
            {
                var gsResult = string.IsNullOrEmpty(name) ? g.GenerateAll() : g.GenerateSingle(name);

                result = new ServiceResult(gsResult.Success, gsResult.Message);
            }

            return result.Serialize();
        }
    }
}